package sort.quicksort;

import java.util.Arrays;
import java.util.Random;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * User:Mr.Du
 * Date:2021/6/16
 * Time:22:37
 */
public class QuickSort {


    public static void main(String[] args) {

        int[] arr = new int[10];
        Random r = new Random();

        for(int j = 0;j<10;j++){
            for(int i = 0;i<arr.length;i++){
                arr[i] = r.nextInt(50);
            }
            System.out.println("原数组: ");
            System.out.println(Arrays.toString(arr));
            quickSort(arr,0,arr.length-1);
            System.out.println("快速排序后的数组: ");
            System.out.println(Arrays.toString(arr));
        }

    }

    private static void quickSort(int[] arr, int i, int j) {
        int start = i;
        int end = j;
        int t = 0;
        if(start < end){
            t = arr[start];
            while(start<end){
                while(t<=arr[end] && start<end){
                    end--;
                }
                if(start<end){
                    arr[start] = arr[end];
                    start++;
                }
                while(t>=arr[start] && start < end){
                    start++;
                }
                if(start<end){
                    arr[end] = arr[start];
                    end--;
                }
            }
            arr[start] = t;
            quickSort(arr,i,start-1);
            quickSort(arr,start+1,j);
        }
    }




}
